const pool = require('../conf/db')

// 获取任务列表
const getTaskByUserId = async (userId) => {
  const connection = await pool.getConnection()
  try {
    const [rows] = await connection.query(
      `SELECT * from task WHERE userId = ${userId} and type = 2 order by deadline ASC;`
    )
    return rows
  } catch (err) {
    throw new Error('获取任务列表失败')
  } finally {
    connection.release()
  }
}

// 获取任务模板列表
const getTaskModel = async () => {
  const connection = await pool.getConnection()
  try {
    const [rows] = await connection.query(`SELECT * FROM taskModel`)
    return rows
  } catch (err) {
    throw new Error('获取任务模板列表失败')
  } finally {
    connection.release()
  }
}

// 获取记事本列表
const getNoteByUserId = async (userId) => {
  const connection = await pool.getConnection()
  try {
    const [rows] = await connection.query(
      `SELECT * FROM task WHERE type = 1 and userId = ${userId};`
    )
    return rows
  } catch (err) {
    throw new Error('获取记事本列表失败')
  } finally {
    connection.release()
  }
}

// 删除任务
const deleteTaskById = async (id) => {
  const connection = await pool.getConnection()
  try {
    return await connection.execute(`DELETE FROM task WHERE id = ${id};`)
  } catch (err) {
    throw new Error('删除任务失败')
  } finally {
    connection.release()
  }
}

// 删除任务
const delTaskModel = async (id) => {
  const connection = await pool.getConnection()
  try {
    return await connection.execute(`DELETE FROM taskModel WHERE id = ${id};`)
  } catch (err) {
    throw new Error('删除任务模板失败')
  } finally {
    connection.release()
  }
}
// 新增任务
const addNewTask = async (params) => {
  const { userId, content, createTime, deadline, type } = params
  const connection = await pool.getConnection()
  const sql1 = 'INSERT INTO task (content,createTime,deadline,type,userId ) VALUES (?,?,?,?,?);'
  try {
    return await connection.execute(sql1, [content, createTime, deadline, type, userId])
  } catch (err) {
    throw new Error('新增任务失败')
  } finally {
    connection.release()
  }
}

// 新增记事本
const addNewNote = async (params) => {
  const { content, userId } = params
  const connection = await pool.getConnection()
  const sql1 = 'INSERT INTO task (content,type,userId ) VALUES (?,?,?);'
  try {
    return await connection.execute(sql1, [content, 1, userId])
  } catch (err) {
    throw new Error('新增记事本失败')
  } finally {
    connection.release()
  }
}
// 新增任务模板
const addNewTaskModel = async (params) => {
  const { content } = params
  const connection = await pool.getConnection()
  const sql = 'INSERT INTO taskModel (content) VALUES (?);'
  try {
    return await connection.execute(sql, [content])
  } catch (err) {
    throw new Error('新增任务模板失败')
  } finally {
    connection.release()
  }
}

// 修改任务
const updateTask = async (params) => {
  const { paramName, taskId, paramValue, finishedTime } = params
  const connection = await pool.getConnection()
  try {
    if (paramName == 'status')
      return await connection.execute(
        `UPDATE task SET ${paramName} = '${paramValue}', finishedTime = '${finishedTime}' WHERE id = ${taskId};`
      )
    else
      return await connection.execute(
        `UPDATE task SET ${paramName} = '${paramValue}' WHERE id = ${taskId};`
      )
  } catch (err) {
    throw new Error('更新状态失败')
  } finally {
    connection.release()
  }
}

// 分发任务
const handOutTask = async (params) => {
  const userIds = params.userIds.split(',')
  const contents = params.contentList.split(',')
  const createTime = params.createTime
  const deadline = params.deadline
  const result = []

  for (index1 in userIds) {
    for (index2 in contents) {
      result.push(`('${contents[index2]}', '${createTime}', '${deadline}', 2, ${userIds[index1]})`)
    }
  }
  const connection = await pool.getConnection()
  try {
    const values = result.join(',')
    const sql = `INSERT INTO task (content,createTime,deadline,type,userId ) VALUES ${values}`
    return await connection.execute(sql)
  } catch (err) {
    throw new Error('分发任务失败')
  } finally {
    connection.release()
  }
}

module.exports = {
  getTaskByUserId,
  getTaskModel,
  handOutTask,
  getNoteByUserId,
  updateTask,
  deleteTaskById,
  addNewNote,
  addNewTask,
  addNewTaskModel,
  delTaskModel
}
